package tgc.edu.library.custom;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

public class CommonController<T,ID,Form extends BaseForm<ID>> {
	@Autowired
	private CommonService<T, ID> baseService;
	@SuppressWarnings("unchecked")
	private Class<T> clazz=GenericsUtils.getSuperClassGenricType(getClass());
	
	@RequestMapping(value="/manage")
	public void manage() {
		
	}
	

	public Specification<T> buildSpec(Form form) {
		return null;
	}

	@RequestMapping(value="/page")
	@ResponseBody
	public Object page(Form form,DataGridParam param) {
		Pageable pageable = param.buildPageable();
		Specification<T> spec=buildSpec(form);
		Page<T> page=baseService.findAll(spec, pageable);
		return EasyuiUtils.buildDataGridResult(page);
	}
	
	/**
	 * 数据加载
	 * @param pageParam
	 * @param form
	 * @return
	 */
	@RequestMapping(value="/page1")
	@ResponseBody
	public Object page1(TablePageable pageParam, Form form) {
		PageRequest pageable = pageParam.bulidPageRequest();
		Specification<T> spec = buildSpec(form);
		Page<T> page=baseService.findAll(spec, pageable);
		return AjaxResult.bulidPageResult(page);
	}
	
	@RequestMapping(value="/edit")
	public void edit(ID id,ModelMap map) throws InstantiationException, IllegalAccessException {
		try {
            T model=clazz.newInstance();
            if(id!=null) {
            	model=baseService.findById(id);
            }
            map.put("model", model);
        } catch (Exception e) {
            // e.printStackTrace();
        }
	}
	
	@RequestMapping(value="/search")
	public void search(Form form,ModelMap map) {
		map.put("model", form);
	}
	
	@RequestMapping(value="/save")
	@ResponseBody
	public Object save(Form form)  {
		T model;
		try {
			model = clazz.newInstance();
			@SuppressWarnings("unchecked")
			ID id=(ID) form.getId();
			if(id!=null) {
				model=baseService.findById(id);
			}
			BeanUtils.copyProperties(form, model,"id");
			baseService.save(model);
			return new EasyuiResult("数据保存成功");
		} catch (InstantiationException | IllegalAccessException e) {
			return new EasyuiResult(false,"数据保存失败");
		}
	}
	
	@RequestMapping(value="/delete")
	@ResponseBody
	public Object delete(ID id) {
		try {
			baseService.deleteById(id);
			return new EasyuiResult("数据删除成功");
		} catch (Exception e) {
			return new EasyuiResult("数据删除失败");
		}
	}
	
	@RequestMapping(value="/combo")
	@ResponseBody
	public Object combo() {
		List<T> list = baseService.findAll();
		return list;
	}

}
